# coding: utf-8

from django.db import models
from django.contrib.auth.models import AbstractUser
from django.conf import settings
from DjangoUeditor.models import UEditorField
import json

class string_with_title(str):
    def __new__(cls,value,title):
        instance = str.__new__(cls, value)
        instance._title = title
        return instance
    def title(self):
        return self._title
    __copy__ = lambda self: self
    __deepcopy__ = lambda self, memodict: self

#create your models here
STATUS ={
    0: u'正常',
    1: u'草稿',
    2: u'删除',
}

class Nav(models.Model):
    name = models.CharField(max_length=40,verbose_name=u'导航条内容')
    url = models.CharField(max_length=200,blank=True,null=True,verbose_name=u'指向地址')
    status =models.IntegerField(default=0,choices=STATUS.items(),verbose_name='状态')
    create_time = models.DateTimeField(u'创建时间',auto_now_add=True)
    class Meta:
        verbose_name_plural = verbose_name = u'导航条'
        ordering = ['-create_time']
        app_label = string_with_title('store',u'博客管理')
    def __unicode__(self):
        return self.name
class Cate(models.Model):
    name = models.CharField(max_length=40,verbose_name=u'名称')
    parent = models.ForeignKey('self',default=None,blank=True,null=True,verbose_name=u'上级分类')
    rank = models.IntegerField(default=0,verbose_name=u'排序')
    status = models.IntegerField(default=0,choices=STATUS.items(),verbose_name='状态')
    create_time = models.DateTimeField(u'创建时间',auto_now_add=True)
    class Meta:
        verbose_name_plural = verbose_name =u'文章分类'
        ordering = ['rank','-create_time']
        app_label = string_with_title('store',u'博客管理')

    def __unicode__(self):
        if self.parent:
            return '%s-->%s'%(self.parent,self.name)
        else:
            return '%s'%(self.name)

class Article(models.Model):
    author = models.ForeignKey(settings.AUTH_USER_MODEL,verbose_name=u'作者')
    category = models.ForeignKey(Cate,verbose_name=u'文章分类')
    title = models.CharField(max_length=100,verbose_name=u'标题')
    en_title = models.CharField(max_length=100,verbose_name=u'英文标题')
    img = models.ImageField(upload_to='ad/%Y/%m', verbose_name='图片路径')
    tags  =  models.CharField(max_length=200,null=True,blank=True,verbose_name=u'标签',help_text=u'用逗号分隔')
    summary = models.TextField(verbose_name=u'摘要')
    content = UEditorField('内容', height=300, width=1000,
                           default=u'', blank=True, imagePath="uploads/images/",
                           toolbars='besttome', filePath='uploads/files/')

    view_times = models.IntegerField(default=0)
    zan_times = models.IntegerField(default=0)

    is_top = models.BooleanField(default=False,verbose_name=u'置顶')
    rank = models.IntegerField(default=0,verbose_name=u'排序')
    status = models.IntegerField(default=0,choices=STATUS.items(),verbose_name='状态')

    pub_time = models.DateTimeField(default=False,verbose_name=u'发布时间')
    create_time = models.DateTimeField(u'创建时间',auto_now_add=True)
    update_time = models.DateTimeField(u'更新时间',auto_now=True)

    def get_tags(self):
        return self.tags.split(',')
    class Meta:
        verbose_name_plural = verbose_name = u'文章'
        ordering = ['rank','-is_top','-pub_time','-create_time','category']
        app_label = string_with_title('store',u'博客管理')




#案例分类
class Cateanli(models.Model):
                name = models.CharField(max_length=40, verbose_name=u'分类名称',unique=True)

                index = models.IntegerField(default=1, verbose_name='分类的排序')
                parent_category = models.ForeignKey('self', verbose_name='父级分类', blank=True, null=True,on_delete=models.CASCADE)

                class Meta:
                    verbose_name = '产品分类'
                    verbose_name_plural = verbose_name
                    ordering = ['index', 'id']

                def __unicode__(self):
                    return self.name
#合作案例
class Anli(models.Model):

    name = models.CharField(max_length=100,verbose_name=u'标题')
    category = models.ForeignKey(Cateanli, verbose_name=u'案例分类')
    img = models.ImageField(upload_to='ad/%Y/%m', verbose_name='图片路径')


    content = UEditorField('内容', height=300, width=1000,
                           default=u'', blank=True, imagePath="uploads/images/",
                           toolbars='besttome', filePath='uploads/files/')

    status = models.IntegerField(default=0,choices=STATUS.items(),verbose_name='状态')
    rank = models.IntegerField(default=0, verbose_name=u'排序')

    class Meta:
        verbose_name = '案例'
        verbose_name_plural = verbose_name
        ordering = ['id']

    def __unicode__(self):
        return self.category.name
# 用户
class User(AbstractUser):
    qq = models.CharField(max_length=20, blank=True, null=True, verbose_name='QQ号码')
    mobile = models.CharField(max_length=11, blank=True, null=True, unique=True, verbose_name='手机号码')

    class Meta:
        verbose_name = '用户'
        verbose_name_plural = verbose_name
        ordering = ['-id']

    def __str__(self):
        return self.username
#广告 _
class Carousel(models.Model):
    title = models.CharField(max_length=100,verbose_name=u'标题')
    summary = models.TextField(blank=True,null=True,verbose_name=u'摘要')
    img = models.ImageField(upload_to='ad/%Y/%m', verbose_name='图片路径')

    create_time = models.DateTimeField(u'创建时间',auto_now_add=True)
    class Meta:
        verbose_name_plural= verbose_name= u'轮播'
        ordering = ['-create_time']
        app_label = string_with_title('store',u'轮播管理')

#产品分类
class Category(models.Model):

    name = models.CharField(max_length=30, verbose_name='分类名称',unique=True)

    index = models.IntegerField(default=1,verbose_name='分类的排序')
    parent_category = models.ForeignKey('self',verbose_name='父级分类',blank=True,null=True,on_delete=models.CASCADE)

    class Meta:
        verbose_name = '产品分类'
        verbose_name_plural = verbose_name
        ordering = ['index','id']

    def __unicode__(self):

        return self.name




#产品等
class Clothing(models.Model):
    category = models.ForeignKey(Category, verbose_name='产品分类')
    name = models.CharField(max_length=30, verbose_name='名称')


    old_price = models.FloatField(default=0.0, verbose_name='原价')
    new_price = models.FloatField(default=0.0, verbose_name='现价')
    discount = models.FloatField(default=1, verbose_name='折扣')

    sales = models.IntegerField(default=0, verbose_name='销量')

    num = models.IntegerField(default=0, verbose_name='库存')
    image_url_i = models.ImageField(upload_to='clothing/%Y/%m', default= 'clothing/default.jpg', verbose_name='展示图片路径')

    image_url_c = models.ImageField(upload_to='clothing/%Y/%m', default= 'clothing/ce.jpg', verbose_name='购物车展示图片')
    content = UEditorField('详情', height=300, width=1000,
                           default=u'', blank=True, imagePath="uploads/images/",
                           toolbars='besttome', filePath='uploads/files/')
    class Meta:
        verbose_name = '商品'
        verbose_name_plural = verbose_name
        ordering = ['id','sales']

    def __unicode__(self):
        return  self.category.name

#购物车条目
class Caritem(models.Model):
    clothing = models.ForeignKey(Clothing, verbose_name='购物车中产品条目')
    quantity = models.IntegerField(default=0, verbose_name='数量')
    sum_price = models.FloatField(default=0.0, verbose_name='小计')

    class Meta:
        verbose_name = '购物车条目'
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return str(self.id)

#购物车
class Cart(object):
    def __init__(self):
        self.items = []
        self.total_price = 0.0

    def add(self, clothing):
        self.total_price += clothing.new_price
        for item in self.items:
            if item.clothing.id == clothing.id:
                item.quantity += 1
                item.sum_price += clothing.new_price
                return
        else:
            self.items.append(Caritem(clothing=clothing, quantity=1, sum_price=clothing.new_price))



